<?php

/**
 * Class Stop
 * 路由拦截器
 */
class Stop
{
    //调度拦截器验证
    public static function ChckStop($stop)
    {
        if (is_string($stop)) {
            self::check($stop);
        }
        //数组多个验证器
        if (is_array($stop)) {
            foreach ($stop as $item) {
                self::check($item);
            }
        }
    }

    //执行验证拦截器
    private static function check($name)
    {
        $path_info = explode('.', $name);
        if (count($path_info) != 2) {
            return msg_no('路由拦截器定义的参考写法【user.auth】', '路由拦截器定义错误：' . $name);
        }
        self::load($path_info[0]);//1：文件名 2：文件绝对地址 $path_info
        $func = $path_info[1];
        //加载到了拦截器文件
        $object = $path_info[0];
        $object = new $object();
        if (method_exists($object, $func)) {
            return $object->$func();
        }
        return msg_no('请在【' . $path_info[0] . '.stop.php' . '】定义拦截器：' . $func, '拦截器不存在：' . $func);
    }

    //加载拦截器
    private static function load($stop_name)
    {
        static $stop;
        $name = $stop_name . '.stop.php';
        $path = ROOT_PATH . 'app' . LINE . 'stop' . LINE . $stop_name . '.stop.php';
        $md_name = 'stop_' . md5($name);
        if (isset($stop[$md_name])) {
            return true;
        }
        if (file_exists($path)) {
            if (!include $path) {
                return msg_no('拦截器加载失败：' . $path, '拦截器加载失败');
            }
            $stop[$md_name] = $name;
            return true;
        }
        return msg_no('拦截器文件不存在：' . $path, '正确的拦截器文件名参考：user.stop.php，拦截器以.stop.php结尾');
    }
}